package com.fastloan.mgt.application.base.util;

import com.fastloan.mgt.model.model.ApplicationModel;
import com.fastloan.mgt.model.service.ModelCalculateService;
import com.fastloan.mgt.processcontrol.base.util.BeanFactoryUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author yanwe
 * createTime 2017-11-2017/11/29 10:20
 */
public class ApplicationUtil {

    public static BigDecimal getFinalAmount(String applicationId,BigDecimal auditAmount,BigDecimal applyAmount){
        List<ApplicationModel> modelResultList = BeanFactoryUtil.getBean(ModelCalculateService.class)
                .getModelResult(applicationId);
        List<BigDecimal> moneyList = new ArrayList<>();
        if (!modelResultList.isEmpty()){
            modelResultList.sort(Comparator.comparing(ApplicationModel::getCreateTime));
            ApplicationModel applicationModel = modelResultList.get(0);
            BigDecimal modelResult = applicationModel.getModelResult();
            if (modelResult != null){
                moneyList.add(modelResult);
            }
        }
        if (auditAmount != null){
            moneyList.add(auditAmount);
        }
        if (applyAmount != null){
            moneyList.add(applyAmount);
        }
        moneyList.sort(BigDecimal::compareTo);
        if (!moneyList.isEmpty()){
            return moneyList.get(0);
        }
        return BigDecimal.ZERO;
    }
}
